package com.xiaoxie.boot.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.BookType;
import com.xiaoxie.boot.service.BookTypeService;
import com.xiaoxie.boot.utils.DataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoxie
 * @create 2022-02-14 20:17
 */
@Api(tags = "分类API")
@RestController
@Slf4j
@RequestMapping("/bookType")
public class BookTypeController {

    @Autowired
    private BookTypeService bookTypeService;

    /**
     * 查询所有图书类型信息
     *
     * @param bookType
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAllBookTypes")
    @ApiOperation(value = "分页查询", notes = "查询所有图书类型信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllBookTypes(BookType bookType, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer limit) {
        PageInfo<BookType> bookTypePageInfo = bookTypeService.selectAllBookTypes(bookType, page, limit);
        return DataInfo.ok("成功", bookTypePageInfo.getTotal(), bookTypePageInfo.getList());
    }

    /**
     * 添加时检查图书类型名是否存在
     *
     * @param typeName
     * @return
     */
    @GetMapping("/bookTypeExist/{typeName}")
    @ApiOperation(value = "去重", notes = "添加时检查图书类型名是否存在")
    @ApiOperationSupport(author = "小谢")
    public DataInfo bookTypeExist(@PathVariable String typeName) {
        BookType bookType = bookTypeService.selectByTypeName(typeName);
        if (bookType == null) {
            return DataInfo.ok();
        } else {
            return DataInfo.fail("图书类型名已存在!");
        }
    }

    /**
     * 图书类型添加
     *
     * @param bookType
     * @param request
     * @return
     */
    @PostMapping("/addBookType")
    @ApiOperation(value = "新增", notes = "图书类型添加")
    @ApiOperationSupport(author = "小谢")
    public DataInfo addBookType(@RequestBody BookType bookType, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "添加了图书类型：" + bookType.getTypeName());
        int i = bookTypeService.insertSelective(bookType);
        if (i == 1) {
            log.info("添加成功");
            return DataInfo.ok();
        } else {
            log.info("添加失败");
            return DataInfo.fail("用户添加失败!");
        }
    }

    /**
     * 修改图书类型信息
     *
     * @param bookType
     * @param request
     * @return
     */
    @PutMapping("/editBookType")
    @ApiOperation(value = "修改", notes = "修改图书类型信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo editBookType(@RequestBody BookType bookType, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "修改了图书类型：" + bookType.getTypeName() + "的信息");
        int i = bookTypeService.updateByPrimaryKeySelective(bookType);
        if (i == 1) {
            log.info("修改成功");
            return DataInfo.ok();
        } else {
            log.info("修改失败");
            return DataInfo.fail("用户修改失败!");
        }
    }

    /**
     * 删除图书信息
     *
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteBookType")
    @ApiOperation(value = "删除", notes = "删除图书信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo deleteBookType(String ids, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        List<String> list = Arrays.asList(ids.split(","));
        log.info(name + "试图删除图书类型信息");
        int i = bookTypeService.deleteBookTypeByIds(list);
        if (i >= 1) {
            log.info("删除了：" + i + "条信息");
            return DataInfo.ok();
        } else {
            log.info("删除失败");
            return DataInfo.fail("删除失败");
        }
    }
}
